package com.sino.androiddesign.customView


import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R


/**
 * 画布的基本操作
 *
 */
class Demo2CanvasBase : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_demo2_loading, container, false)
    }
}


/**
 * 位移
 *
 */

class Demo2TestView : View {

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)

    private val mPaint = Paint()

    private var mWidth = 0
    private var mHeight = 0

    init {

    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        /**
         * 位移(translate)
         * 是坐标系的移动,位移是基于当前位置的移动,而不是每次都基于左上角(0.0)点的移动
         */

//        //在坐标圆点绘制一个黑色圆形
//        mPaint.color =Color.BLACK
//        canvas!!.translate(200f,200f)
//        canvas.drawCircle(0f,0f,100f,mPaint)
//
//        //在坐标圆点绘制一个蓝色圆形
//        mPaint.color =Color.GREEN
//        canvas.translate(200f,200f)
//        canvas.drawCircle(0f,0f,100f,mPaint)


        /**
         * 缩放(scale)
         * scale(float sx,float sy)
         * scale(float sx,float sy,float px ,float py)
         * sx sy,分别是x轴和y轴的缩放比例,px,py 用来控制缩放中心点,默认是坐标原点
         */

        canvas!!.translate(mWidth / 2.toFloat(), mHeight / 2.toFloat())

//        val  rect = RectF(0f,-400f,400f,0f)
//        mPaint.color =Color.BLACK
//        canvas.drawRect(rect,mPaint)
//
//        canvas.scale(0.5f,0.5f)
//
//        mPaint.color =Color.BLUE
//        canvas.drawRect(rect,mPaint)
//
//        val rect2 = RectF(-400f, -400f, 400f, 400f)
//
//        mPaint.style = Paint.Style.STROKE
//        mPaint.strokeWidth = 10f
//        mPaint.color = Color.GREEN
//
//        for (i in 0 until 20) {
//            canvas.scale(0.9f, 0.9f)
//            canvas.drawRect(rect2, mPaint)
//        }

        /**
         * 旋转(rotate)
         * rotate(float degrees)
         * rotate(float degrees,float px ,float py)
         * degrees 旋转角度 ,px ,py 旋转中心点,默认是坐标原点
         */

//        val reactF = RectF(0f,-400f,400f,0f)

//        mPaint.color =Color.BLACK
//        canvas!!.drawRect(reactF,mPaint)
//        canvas.rotate(180f)
//
//        mPaint.color =Color.BLUE
//        canvas.drawRect(reactF,mPaint)
//
//        mPaint.style = Paint.Style.STROKE
//        canvas.drawCircle(0f,0f,400f,mPaint)
//        canvas.drawCircle(0f,0f,360f,mPaint)
//        for (i in 0 until 36){
//            canvas.drawLine(0f,360f,0f,400f,mPaint)
//            canvas.rotate(10f)
//        }


        /**
         * 错切(skew)
         * skew(float sx,float sy)
         * sx 将画布在x方向上倾斜相应的角度,sx为倾斜角度的tan值
         * sy 将画布在y方向上倾斜相应的角度,sy为倾斜角度的tan值
         */

        val reactF = RectF(0f,200f,200f,0f)
        mPaint.color =Color.BLACK
        mPaint.style=Paint.Style.STROKE
        canvas!!.drawRect(reactF,mPaint)
        canvas.skew(1f,0f)

        mPaint.color =Color.BLUE
        canvas.drawRect(reactF,mPaint)
    }


}

